import java.sql.*;

/**
 * 传统方法使用jdbc
 */
public class JDBCTest {
    public static void main(String[] args) {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;

        try {
            //注册驱动
            //方法1
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            //方法2 类加载
            //Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc","root","root");
            //获取数据库操作对象
            statement = conn.createStatement();
            //执行Sql
            String sql = "select * from people";
            rs = statement.executeQuery(sql);
            //处理查询结果集
            while (rs.next()){

                String name = rs.getString("name");
                String time = rs.getString("birth");
                System.out.println(name+","+time);
                /**结果集和预期结果一致，一致，一致
                 * 张三,15
                 * 李四,16
                 */
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //释放资源，倒着释放 rs-》statement-》conn
            if (rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
